<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller\haoyayi;

use think\Controller as ThinkController;
use app\api\model\haoyayi\UserInfo as UserInfoModel;

//config('app_trace',true);

class User extends ThinkController {

    public function userNameIsNull() {
        if (isset($_GET['trd_session_key'])) {
            $sessionInfo = explode( '.',$_GET['trd_session_key']);
            $UserInfoModel = new UserInfoModel;
            $data = $UserInfoModel->where('id', $sessionInfo[0])->find();
            if ($data->getData('name')) {
                $ret = array(
                    'status' => 1,
                    'msg' => '已设置用户名',
                    'data' => array(
                        "isNull" => 0,
                        "userName"=>$data->getData('name'),
                        "userLevel"=>$data->getData('vip_level')
                    )
                );
            } else {
                $ret = array(
                    'status' => 1,
                    'msg' => '暂未设置用户名',
                    'data' => array(
                        "isNull" => 1
                    )
                );
            }
        } else {
            $ret = array(
                'status' => 0,
                'msg' => '用户未登录'
            );
        }
        $ret = json_encode($ret);
        //var_dump($ret);
        echo $ret;
        return;
    }

    public function userNameIsUsed($name) {
        $UserInfoModel = new UserInfoModel;
        $data = $UserInfoModel->where('name', $name)->find();
        if (!$data) {
            $ret = array(
                'status' => 1,
                'msg' => '用户名可用',
                'data' => array(
                    "isUsed" => 0
                )
            );
        } else {
            $ret = array(
                'status' => 1,
                'msg' => '用户名已存在',
                'data' => array(
                    "isUsed" => 1
                )
            );
        }
        $ret = json_encode($ret);
        //var_dump($ret);
        echo $ret;
        return;
    }

    public function updateUserName($name,$phoneNum) {
        if (isset($_GET['trd_session_key'])) {
            $sessionInfo = explode( '.',$_GET['trd_session_key']);
            $UserInfoModel =  UserInfoModel::get(['id'=>$sessionInfo[0]]);
            $UserInfoModel->name = $name;
            $UserInfoModel->telephone = $phoneNum;
            $UserInfoModel->save();
            $ret = array(
                'status' => 1,
                'msg' => '更新成功'
            );
        } else {
            $ret = array(
                'status' => 0,
                'msg' => '用户未登录'
            );
        }
        $ret = json_encode($ret);
        //var_dump($ret);
        echo $ret;
        return;
    }
}
